
local jass = require "jass.common"
local data_list = load_excel('数值投放.xlsx','数值强度')
local js_num = {}
for a=1,16 do
    js_num[a] = data_list[a*2]['精英怪数量']
end
--[[
数值获取
]]
local unit_weight = {}
local jy_weight = {}
for k,v in pairs(load_excel('数值投放.xlsx','怪物强度')) do
    local skill = {}
    table.insert(skill,v['技能1'])
    table.insert(skill,v['技能2'])
    v.skill = skill
    v.types = v['类型']

    ac.lni('unit',k,v)
    if v['阶段'] and v['类型']=='普通' then
        local lv = v['阶段']
        for a=lv,4 do
            if not unit_weight[a] then
                unit_weight[a] = {}
            end
            table.insert(unit_weight[a],{k,v['随机权重']})
        end
    end
    if v['阶段'] and v['类型']=='精英' then
        local lv = v['阶段']
        for a=lv,4 do
            if not jy_weight[a] then
                jy_weight[a] = {}
            end
            table.insert(jy_weight[a],{k,v['随机权重']})
        end
    end
end


local mt = {}
mt.hard = 0
--天数
mt.day = 0
--白天
mt.time1 = 30
--黑夜时间
mt.time2 = 120
--黑夜到白天过度
mt.time2_1 = 40
--
mt.unit_count = 0
mt.unit_max = 200
mt.max_level = 16

mt.rect = ac.rect.j_rect('base')
mt.base = mt.rect:center()
mt.kill_count = 0
mt.index = 0
mt.speed = 1
 
--添加怪物
function mt:add_unit(name,point,ex)
    if self.unit_count>=self.unit_max and ex==nil then
        return
    end
    local name = name or table.get_by_weight(self.unit_weight)
    local point = point or self.base - {math.random(360),2500}
    local owner = ac.player[self.index%4 + 9]
    local unit = owner:create_unit(name,point)
    jass.RemoveGuardPosition(unit.handle)
    unit:set_enemy_level(self.day*2)
    unit:issue_order('attack',self.base)
    unit:add_buff '怪物-自动施法'{}

    self.index = self.index + 1
    self.unit_count = self.unit_count + 1
    self.units[unit] = true
    unit:event '单位-死亡'(function()
        self.units[unit] = nil
        self.kill_count = self.kill_count + 1
        self.unit_count = self.unit_count - 1
    end)
    return unit
end

--晚上-开始刷怪
local boss = {
    '黑暗大鱼',
    '深渊触手',
    '娜迦女王',
    '玄武',
    '克苏鲁',
}
local boss_hp = {
    0,60,120,180,
}
function mt:night()
    self:clear()
    if self.day%4==0 then
        local level = self.day
        local last = self.day==self.max_level
        local name = table.remove(boss,math.random(#boss))
        local life_add = boss_hp[ac.player.online_count] or boss_hp[#boss_hp]
        ac.game:notify('游戏-隐藏商店')
        ac.wait(2000,function()
            ac.player.self:cinematic_filter{
                start = {100,0,0,0},
                finish = {100,0,0,100},
                time = 2,
            }
            ac.wait(2000,function()
                jass.DisplayCineFilter(false)
            end)
        end)
        if name=='深渊触手' then
            local count = 8
            for a=1,count do
                local p = self.base - {a * 45,1200}
                local u = self:add_unit(name,p,true)
                u:add('生命上限%',life_add)
                u:add_buff '排行榜'{}
                u:add_buff '怪物-自动施法'{}
                u:event '单位-死亡'(function()
                    count = count - 1
                    if count==0 then
                        ac.game:notify('游戏-击杀Boss',self,level)
                        if last then
                            self:clear()
                            ac.game:notify('游戏-游戏胜利',self)
                        end
                    end
                end)
            end
        else
            local u = self:add_unit(name,self.base,true)
            
            u:add('生命上限%',life_add)
            u:add_buff '排行榜'{}
            u:add_buff 'Boss出场'{time = 4}
            u:add_buff '怪物-自动施法'{}
            u:event '单位-死亡'(function()
                ac.game:notify('游戏-击杀Boss',self,level)
                if last then
                    self:clear()
                    ac.game:notify('游戏-游戏胜利',self)
                end
            end)
        end

    end
    if self.day~=self.max_level then
        local ex = 0
        local ex_unit = 0
        self.timer_jl1 = ac.loop(30*1000,function()
            if self.kill_count>50 then
                ex_unit = math.min(ex_unit +1,4)
            end
            if self.day>=5 and math.random(100)<=25 then
                self.speed = self.speed + 1
                ac.player.self:send_msg('|cffff0000深海传来呓语，怪物们暴走了|r')
                ac.wait(30*1000,function()
                    self.speed = self.speed - 1
                end)
            end
            self.kill_count = 0
        end)
        self.timer_jl1:on_timer()

        self.timer_jl2 = ac.loop(1000,function()
            local count = 1 + ex_unit + math.ceil(self.day/4) - 1
            
            count = count * self.speed + ex
            local num = math.floor(count)
            ex = count - num
            self.timer_sb = ac.timer(1000/num,num,function()
                self:add_unit()
            end)
        end)

        local num = js_num[self.day]
        self.exit_timer0 = ac.timer((self.time2/(num+1)*1000),num,function()
            local name = table.get_by_weight(self.jy_unit)
            self:add_unit(name)
        end)
        self.exit_timer1 = ac.wait((self.time2 - self.time2_1)*1000,function()
            ac.player.self:send_msg('白天即将到来！')
            ac.player.self:send_msg('白天即将到来！')
            ac.player.self:send_msg('白天即将到来！')
        end)
    
        self.exit_timer2 = ac.wait(self.time2*1000,function()
            self:sunup()
        end)
        ac.game:notify('游戏-进入黑夜',self.day)
        ac.game:notify('游戏-设置顶部计时',self.title,self.exit_timer2,'夜晚：')
    else
        self.exit_timer2 = ac.wait(120*1000,function()
            self:clear()
            ac.game:notify('游戏-游戏失败',self)
        end)
        ac.game:notify('游戏-设置顶部计时',self.title,self.exit_timer2,'失败：')
    end

end

--白天
function mt:sunup(time)
    local time = time or self.time1
    self:clear()
    self.day = self.day + 1
    self.title = ('难%s | 第%s天'):format(self.hard,self.day)
    local lv = math.ceil(self.day/4)
    self.unit_weight = unit_weight[lv]
    self.jy_unit = jy_weight[lv]
    self.exit_timer1 = ac.wait(time*1000,function()
        self:night()
    end)
    ac.game:notify('游戏-发工资',self.day)
    ac.game:notify('游戏-新的一天',self.day)
    ac.game:notify('游戏-设置顶部计时',self.title,self.exit_timer1,'白天：')
end


function mt:clear()
    if self.exit_timer0 then self.exit_timer0:remove() end
    if self.exit_timer1 then self.exit_timer1:remove() end
    if self.exit_timer2 then self.exit_timer2:remove() end
    if self.timer_jl1 then self.timer_jl1:remove() end
    if self.timer_jl2 then self.timer_jl2:remove() end
    if self.timer_sb then self.timer_sb:remove() end
    --for u in pairs(self.units) do
    --    u:remove()
    --end
end

function mt:init()
    self.units = {}
    self:sunup(10)
end


ac.game:event '游戏-设置难度'(function(_,hard)
    local data = setmetatable({}, {__index = mt})
    ac.enemy = data
    data.hard = hard
    data:init()
end)



local buff = ac.buff['Boss出场']
buff.model2 = [[4ba42c4078ddc07409f0afed579f495d.mdl]]
buff.time = 4
buff.fade = 1
buff.pulse = 0.03
function buff:on_pulse()
    local h = math.min(- ((self:get_remaining())/(self.time - self.fade)) * 400,0)
    self.effect:set_height(h + self.height)
end

function buff:on_add()
    local unit = self.target
    local point = unit:get_point()
    self.height = point:get_height()
    ac.player.self:noise(5,self.time)
    unit:add_restriction '隐藏'
    unit:add_restriction '无敌'
    unit:add_restriction '硬直'
    self.effect = point:effect{
        model = unit:get_model(),
        size = unit:get_slk('modelScale',1) * (unit['模型大小'] or 1),
        hide_when_remove = true,
        angle = 270,
    }
    self:on_pulse()
    self:gc(self.effect)
    self:gc(point:effect{
        model = self.model2,
        size = 1,
    })
    self:gc(function()
        unit:remove_restriction '隐藏'
        unit:remove_restriction '无敌'
        unit:remove_restriction '硬直'
    end)
end


ac.game:event '单位-初始化'(function(_,unit)
    if unit:is_type('精英') then
        unit_add_life2(unit)
    end
    if unit:is_type('Boss') then
        unit_add_life2(unit,2)
    end
end)